<?php
@header('Content-Type:application/json;charset=utf-8');
error_reporting(0);
const ROOT    = __DIR__ . DIRECTORY_SEPARATOR;
const CLASSES = ROOT . 'Classes' . DIRECTORY_SEPARATOR;
define("CACHE_PATH", implode(DIRECTORY_SEPARATOR, [__DIR__, 'Cache', 'lanzou', date('Y-m-d'), '']));
require_once CLASSES . 'common.php';
$conf = require_once 'config.php';
spl_autoload_register(function ($cls) {
	$file = ROOT . str_replace('\\', DIRECTORY_SEPARATOR, $cls) . '.php';
	if (is_file($file))
		require_once $file;
});
$c     = r('c', '');
$id    = r('id', '');
$pwd   = r('pwd', '');
$y     = r('y', '');
$d     = r('d', '');
$url     = r('url', '');
if ($c === 'verify') {
	$verify_conf = $conf['verify'];
	\Classes\Verify::config($verify_conf)->create();
	exit();
}
use Classes\Lanzou;
Lanzou::config($conf);
$ret = msg(-3, '未知操作');$host='';
if(!empty($y))Lanzou::setY($y);
if(!empty($d) && empty($url)){$arr = explode('-',$d);$id=$arr[0];$pwd=$arr[1];}
if(!empty($url) && substr($url,0,4)=='http'){
    $arr = explode('.com/',$url);$id=$arr[1];$host=$arr[0].'.com/';
}
switch ($c) {
	case 's':
		if ($id === '') $ret = msg(-1, '分享id不能为空');
		else $ret = Lanzou::parseUrlByShareId($id, @$pwd,$host);
		break;
	case 'login':
		$pass = r('pass', '');
		$cookie = r('cookiestr', '');
		if ($pass === '')
			$ret = msg(-3, '管理员密码不能为空');
		else
			$ret = Lanzou::login($pass,$cookie);
		break;
	default:
		if ($id === '') break;
		$first = strtolower(substr($id, 0, 1));
		if ($first === 'b')
			$ret = Lanzou::shareList($id, $pwd, $page);
		else if ($first === 'i')
			$ret = Lanzou::parseUrlByShareId($id, $pwd,$host);
		else
			$ret = Lanzou::parseUrl($id);
}
exit(arr2json($ret));